n = int(input())
x = 0
if len(str(n)) == 4:
    for i in range(2, n + 1):
        if str(i) in str(n):
            u = False
            for tet in range(2, i // 2 + 1):
                if i % tet == 0:
                    u = True
                    break
            if u:
                continue
            else:
                x = max(x, i)
print(x)

